package com.mrzhou.core.tool.props;

import lombok.Getter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.Map;

@ConfigurationProperties("cloud")
public class CloudProperties implements EnvironmentAware {

    private Environment env;

    @Getter
    private Map<String, String> prop = new HashMap<>();

    public String get(String key) {
        return get(key, null);
    }

    public String get(String key, String defaultValue) {
        String value = prop.get(key);
        if(value == null) {
            return defaultValue;
        }
        return value;
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.env = environment;
    }

    /**
     *  获取项目运行环境
     */
    public String getEnv() {
        Assert.notNull(env, "SpringBoot下Environment不可能为空！");
        return env.getProperty("spring.profiles.active", "dev");
    }

    /**
     * 获取项目名称
     */
    public String getApplicationName() {
        Assert.notNull(env, "SpringBoot下Environment不可能为空！");
        return env.getProperty("spring.application.name", "");
    }

}
